//
//  TYSwitchViewViewController.m
//  TYUIKit_Example
//
//  Created by 谢航宇_WORK on 2022/3/17.
//  Copyright © 2022 谢航宇. All rights reserved.
//

#import "TYSwitchViewViewController.h"
#import "TYSwitchView.h"
#import "Masonry.h"
#import "UIColor+TYColor.h"
@interface TYSwitchViewViewControllerCell : UICollectionViewCell
@property (nonatomic,retain) UILabel * titleLab;
@end

@implementation TYSwitchViewViewControllerCell
- (UILabel *)titleLab {
    if (!_titleLab) {
        __weak typeof(self)weakSelf = self;
        _titleLab = [[UILabel alloc]init];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(weakSelf);
        }];
    }
    return _titleLab;
}
@end

@interface TYSwitchViewViewController ()<TYSwitchViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) NSArray * titleAry;
@end

@implementation TYSwitchViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleAry = @[@"zzzzzzzz",@"xx",@"ccccc",@"bbbbbbbbbbbbb",@"nnnn"];
    [self switchView1];
    [self switchView2];
    [self switchView3];
}

- (void)switchView1 {
     __weak typeof(self)weakSelf = self;
    TYSwitchView * switchView = [[TYSwitchView alloc]init];
    switchView.delegate = self;
    switchView.backgroundColor = [UIColor whiteColor];
    switchView.type = TYSwitchViewSeparatorTypeDivide;
    [switchView registerClass:[TYSwitchViewViewControllerCell class] forCellWithReuseIdentifier:@"TYSwitchViewViewControllerCell"];
    [self.view addSubview:switchView];
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view).offset(44);
        make.height.mas_equalTo(50);
    }];
}

- (void)switchView2 {
     __weak typeof(self)weakSelf = self;
    TYSwitchView * switchView = [[TYSwitchView alloc]init];
    switchView.delegate = self;
    switchView.backgroundColor = [UIColor whiteColor];
    switchView.type = TYSwitchViewSeparatorTypeAutoSize;
    [switchView registerClass:[TYSwitchViewViewControllerCell class] forCellWithReuseIdentifier:@"TYSwitchViewViewControllerCell"];
    [self.view addSubview:switchView];
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view).offset(122);
        make.height.mas_equalTo(50);
    }];
}

- (void)switchView3 {
     __weak typeof(self)weakSelf = self;
    TYSwitchView * switchView = [[TYSwitchView alloc]init];
    switchView.delegate = self;
    switchView.backgroundColor = [UIColor whiteColor];
    switchView.type = TYSwitchViewSeparatorTypeCustom;
    [switchView registerClass:[TYSwitchViewViewControllerCell class] forCellWithReuseIdentifier:@"TYSwitchViewViewControllerCell"];
    [self.view addSubview:switchView];
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view).offset(200 );
        make.height.mas_equalTo(50);
    }];
}


- (NSInteger)numberOfRowOfSwitchHeaderView:(TYSwitchView *)headerView {
    return self.titleAry.count;
}


- (NSAttributedString *)switchHeaderView:(TYSwitchView *)headerView titleForRow:(NSInteger)row isSelected:(BOOL)selected {
    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:self.titleAry[row] attributes: @{
        NSFontAttributeName: [UIFont systemFontOfSize:15],
        NSForegroundColorAttributeName: headerView.index == row?[UIColor redColor]:[UIColor blueColor]
    }];
    return attrString;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TYSwitchViewViewControllerCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TYSwitchViewViewControllerCell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleAry[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(100, 40);
}

- (void)switchHeaderView:(TYSwitchView *)headerView backgroundView:(UIImageView *)backgroundView forRow:(NSInteger)row isSelected:(BOOL)selected {
    UIView * bottomLine = [backgroundView viewWithTag:100];
    
    if (!bottomLine) {
        bottomLine = [[UIView alloc]init];
        bottomLine.tag = 100;
        [backgroundView addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.bottom.mas_equalTo(backgroundView);
            make.width.mas_equalTo(25);
            make.height.mas_equalTo(3);
        }];
        bottomLine.backgroundColor = [UIColor redColor]; 
    }
    bottomLine.hidden = !selected;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
